
package com.canyinghao.cananimation.demo;


import com.canyinghao.cananimation.CanAnimation;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.TextAlignment;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


/**
 * @ProjectName: CanAnimation
 * @ClassName: AnimationAbility
 * @Description: 普通动画
 * @Author: lr
 * @CreateDate: 2021/2/24 10:33
 */
public class AnimationAbility extends Ability {
    StackLayout animContainer;
    Button start_animator;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_animator);
        start_animator = (Button) findComponentById(ResourceTable.Id_start_animator);
        animContainer = (StackLayout) findComponentById(ResourceTable.Id_animContainer);

        start_animator.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                start_animator.setVisibility(Component.HIDE);
                playAnimations();
            }
        });

        animContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                playAnimations();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    private void playAnimations() {
        animContainer.removeAllComponents();

        Point center = new Point((float) animContainer.getWidth() / 2, (float) animContainer.getHeight() / 2);
        int size = 70;

        for (int i = 0; i < 6; i++) {
            StackLayout.LayoutConfig params = new StackLayout.LayoutConfig(size, size);
            final Text view = new Text(this);
            view.setLayoutConfig(params);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.BLUE.getValue()));
            view.setBackground(shapeElement);
            view.setText("" + i);
            view.setTextColor(Color.WHITE);
            view.setTextAlignment(TextAlignment.CENTER);
            view.setTextSize(20);

            Point newPos = new Point();
            switch (i) {
                case 0:
                    newPos.modify(0, 0);
                    break;

                case 1:
                    newPos.modify(0, center.getPointY());
                    break;

                case 2:
                    newPos.modify(0, center.getPointY() * 2 - size);
                    break;

                case 3:
                    newPos.modify(center.getPointX() * 2 - size, 0);
                    break;

                case 4:
                    newPos.modify(center.getPointX() * 2 - size, center.getPointY());
                    break;

                case 5:
                    newPos.modify(center.getPointX() * 2 - size, center.getPointY() * 2 - size);
                    break;
                default:
            }
            animContainer.addComponent(view);
            int finalI = i;
            AnimatorProperty animatorValue = view.createAnimatorProperty();
            animatorValue.moveToX(newPos.getPointX()).moveToY(newPos.getPointY());
            animatorValue.setDuration(2000);
            Animator end = CanAnimation.run(new Runnable() {
                @Override
                public void run() {
                    if (finalI == 5) {
                        start_animator.setVisibility(Component.VISIBLE);
                    }
                }
            });

            CanAnimation.animSmall2Big(view, animatorValue, end).start();
        }
    }
}
